package org.jcut.controller;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.jcut.data.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class MyException {
	@Autowired
	Data data;
	@InitBinder
	protected void initBinder(WebDataBinder binder) {
	    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	    binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
	}

	@ResponseBody
	@ExceptionHandler
	public void myException(Exception e) throws Exception {
		
		String mes=e.getMessage();
		data.setData(mes);
		data.setMsg("全局异常！");
		data.setStatus("0");
		
		//return data;
		throw e;
	}
}
